﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ShiXuLong.Model;
using ShiXuLong.Service;

namespace ShiXuLong.Service
{
    public class Guide
    {
        ShoesEntities db = new ShoesEntities();
        /// <summary>
        /// 多条件查询
        /// </summary>
        /// <param name="limit"></param>
        /// <param name="page"></param>
        /// <param name="shopId"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public List<GuideCus> GetGuide(int limit, int page, string shopName, out int count)
        {

            var list = from item in db.Guides
                       join shops in db.Shops
                       on item.ShopId equals shops.Id
                       select new GuideCus
                       {
                           Id = item.Id,
                           ShopId = item.Id,
                           StartTime = item.StartTime,
                           ShopName = shops.ShopName,
                           EndTime = item.EndTime,
                           Price = item.Price,
                           Num = item.Num
                       };


            // var list = db.Guides.Where(p => true).ToList();
            //var list = from item in db.Guides.Where(p => true)
            //           select new GuideCus
            //           {
            //               Id = item.Id,
            //               ShopId = item.Id,
            //               StartTime = item.StartTime,
            //               //ShopName = item.Shops.Where(p => p.Id == item.ShopId),
            //               EndTime = item.EndTime,
            //               Price = item.Price,
            //               Num = item.Num
            //           };
            if (!string.IsNullOrWhiteSpace(shopName))
            {
                //int id = int.Parse(ShopName);
                list = list.Where(p => p.ShopName.Contains(shopName));
            }
            count = list.Count();

            return list.ToList();
        }
        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="guide"></param>
        /// <returns></returns>
        public int AddGuide(Model.Guide guide)
        {
            db.Guides.Add(guide);
            int res = db.SaveChanges();
            return res;
        }
        /// <summary>
        /// 根据id获取数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Model.GuideCus GetGuideById(int id)
        {
            var list = from item in db.Guides.Where(p => p.Id == id)
                       select new GuideCus
                       {
                           Id = item.Id,
                           ShopId = item.Id,
                           //ShopName = item.Shops.Where(p => p.Id == item.ShopId).SingleOrDefault().ShopName,
                           StartTime = item.StartTime,
                           EndTime = item.EndTime,
                           Price = item.Price,
                           Num = item.Num
                       };

            return list.SingleOrDefault();
        }
        public int EditGuide(Model.Guide guide)
        {
            db.Entry(guide).State = System.Data.Entity.EntityState.Modified;
            int res = db.SaveChanges();
            return res;
        }
        public int DelGuide(int id)
        {
            var guide = db.Guides.Where(p => p.Id == id).ToList();
            var cd = guide[0];
            db.Guides.Remove(cd);
            var res = db.SaveChanges();
            return res;
        }
    }
}
